@echo off

echo ***** BEGIN CONFIG *****
echo.
:: set local path
SET JAVA_BIN=Y:\j2sdk1.4.2
SET J2ME_DIR=Y:\WTK22
SET CLASSES_DIR=%cd%\classes
SET SOURCE_DIR=%cd%\src
SET RELEASE_DIR=%cd%\bin
SET RESOURCE_DIR=%cd%\res
SET TEM_CLASSES=%cd%\tmpclass
SET TEM_LIB=%cd%\tmplib
SET EMULATOR_PATH=Y:\GLemulator1.1.54

:: set for midlet

SET CLDC_VERSION=CLDC-1.1
SET MIDP_VERSION=MIDP-2.0

::change 2 variable below for each midlet
SET MIDLET_NAME=CanvasRectangleExample
SET MIDLET_CLASS=RectangleExample


if not exist %CLASSES_DIR% (
	md classes
	echo ##### Created classes folder ##### 
)

if not exist %RELEASE_DIR% (
	md bin
	echo ##### Created bin folder #####
)

if not exist %TEM_CLASSES% (
	md tmpclass
	echo ##### Created tmpclass folder ##### 
)

if not exist %TEM_LIB% (
	md tmplib
	echo ##### Created tmplib folder ##### 
)

if not exist %EMULATOR_PATH% (
	echo ##### NO EMULATOR TO RUN #####
	pause
	goto :EOF
)

if "%MIDP_VERSION%"=="MIDP-2.0" (
	SET BUILD_LIB=%J2ME_DIR%\lib\midpapi20.jar
) else (
	SET BUILD_LIB=%J2ME_DIR%\lib\midpapi10.jar
)
if "%CLDC_VERSION%"=="CLDC-1.1" (
	SET BUILD_LIB=%BUILD_LIB%;%J2ME_DIR%\lib\cldcapi11.jar
) else (
	SET BUILD_LIB=%BUILD_LIB%;%J2ME_DIR%\lib\cldcapi10.jar
)
cd %RELEASE_DIR%
echo *** Generating MANIFEST.MF ***
echo.
echo Manifest-Version: 1.0> MANIFEST.MF
echo MIDlet-Name: %MIDLET_NAME%>> MANIFEST.MF
echo MIDlet-Version: 1.0>> MANIFEST.MF
echo MIDlet-Vendor: Mr.D>> MANIFEST.MF
echo MIDlet-Icon: /icon.png>> MANIFEST.MF
echo MIDlet-1: %MIDLET_NAME%, /icon.png, %MIDLET_CLASS%>> MANIFEST.MF
echo MicroEdition-Configuration: "%CLDC_VERSION%">>MANIFEST.MF
echo MicroEdition-Profile: %MIDP_VERSION%>>MANIFEST.MF
echo ### Created MANIFEST.MF ###
cd..
echo.
echo *** Compiling source files ***
echo.
%JAVA_BIN%\bin\javac.exe -target 1.1 -bootclasspath %BUILD_LIB% -d %TEM_CLASSES% %SOURCE_DIR%\*.java
if %ERRORLEVEL%==1 (	
	goto ERROR
) else (
	echo.
	echo ##### COMPILE SUCCESS #####
	echo.
)
%J2ME_DIR%\bin\preverify.exe -classpath %BUILD_LIB% -d %CLASSES_DIR% %TEM_CLASSES%
if %ERRORLEVEL%==1 (
	goto ERROR
) else (
	echo.
	echo ##### PREVERIFY SUCCESS #####
	echo.
)
%JAVA_BIN%\bin\jar.exe -cvfm %RELEASE_DIR%\%MIDLET_NAME%.jar %RELEASE_DIR%\MANIFEST.MF -C %CLASSES_DIR% .
if %ERRORLEVEL%==1 (
	goto ERROR
) else (
	echo.
	echo ##### CREATE JAR SUCCESS #####
	echo.
)
:BUILDJAR
cd %RELEASE_DIR%
call :GET_JAR_SIZE %MIDLET_NAME%.jar
goto :EOF
:GET_JAR_SIZE
set jarsize=%~z1
:GENERATE_JAD
echo *** Generating %MIDLET_NAME%.jad ***
echo MIDlet-Name: %MIDLET_NAME%> %MIDLET_NAME%.jad
echo MIDlet-Version: 1.0>> %MIDLET_NAME%.jad
echo MIDlet-Vendor: Mr.D>> %MIDLET_NAME%.jad
echo MIDlet-Icon: /icon.png>> %MIDLET_NAME%.jad
echo MIDlet-1: %MIDLET_NAME%, /icon.png, %MIDLET_CLASS%>> %MIDLET_NAME%.jad
echo MIDlet-Description: Some simple effect in J2ME>> %MIDLET_NAME%.jad
echo MIDlet-Jar-URL: %MIDLET_NAME%.jar>> %MIDLET_NAME%.jad
echo MIDlet-Data-Size: 1024>> %MIDLET_NAME%.jad
echo MIDlet-Jar-Size: %jarsize%>> %MIDLET_NAME%.jad
del MANIFEST.MF
cd..
echo.
echo ##### BUILD JAD COMPLETE #####
echo.
REM pause
:RUN
REM cd
REM run with default emulator

REM SET XDevice=DefaultColorPhone
REM %J2ME_DIR%\bin\emulator.exe -Xdevice:%XDevice% -Xdescriptor:%RELEASE_DIR%\%MIDLET_NAME%.jad
REM goto EOF

REM run with GLEmulator
set jar=%RELEASE_DIR%\%MIDLET_NAME%.jar
echo %jar%
%EMULATOR_PATH%\glemulator.exe -cp ./glemulator.jar -Dmicroedition.locale=en emulator.GLEmulator -Dvk_rsk=VK_F2 -Dvk_lsk=VK_F1 -dSCREEN_HEIGHT=320 -dSCREEN_WIDTH=240 -device "Sony-Ericsson K800i" -systemfont "Arial Unicode MS" -jar %jar%
cd ..
goto :EOF

:ERROR
cd.
cd ##### HAVE SOMETHING WRONG ######
pause
goto :EOF